/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.proescola.dao;

import br.com.proescola.modelo.Aluno;
import br.com.proescola.modelo.Responsavel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Joao
 */
public class ResponsavelDAO {

    private Connection conexao;
    //private PreparedStatement stmt;

    public ResponsavelDAO() throws SQLException {
        this.conexao = CriaConexao.getConexao(); //Istancia um objeto Connection, que é o parâmetro da classe CriaConexao.java
    }

    public void cadastrar(Responsavel r1) throws SQLException {
        //Prepara a estrutura da Query e declara o objeto PreparedStatement
        String sql = "INSERT INTO responsavel (nm_responsavel, rg_responsavel, cpf_responsavel, telefone_responsavel, celular_responsavel) VALUES (?,?,?,?,?)";
        PreparedStatement stmt = conexao.prepareStatement(sql);
        String ret = "erro";
        //Seta os valores que vão ser inseridos na Query
        stmt.setString(1, r1.getNome());
        stmt.setString(2, r1.getRg());
        stmt.setString(3, r1.getCpf());
        stmt.setString(4, r1.getTelefone());
        stmt.setString(5, r1.getCelular());
        //Executa a inserção SQL
        stmt.execute();
        stmt.close();

    }

    public List<Responsavel> getLista() throws SQLException {
        String sql = "SELECT * FROM responsavel";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();

        List<Responsavel> responsaveis = new ArrayList<Responsavel>();
        while (rs.next()) {
            Responsavel r1 = new Responsavel(rs.getInt("cd_responsavel"), rs.getString("nm_responsavel"), rs.getString("rg_responsavel"), rs.getString("cpf_responsavel"), rs.getString("telefone_responsavel"), rs.getString("celular_responsavel"));
            responsaveis.add(r1);
        }
        rs.close();
        stmt.close();
        return responsaveis;
    }

    public List<Responsavel> getById(String x) throws SQLException {
        String sql = "SELECT * FROM responsavel WHERE nm_responsavel LIKE ?";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        stmt.setString(1, ("%" + x + "%"));
        ResultSet rs = stmt.executeQuery();

        List<Responsavel> responsaveis = new ArrayList<Responsavel>();
        while (rs.next()) {
            Responsavel r1 = new Responsavel(rs.getInt("cd_responsavel"), rs.getString("nm_responsavel"), rs.getString("rg_responsavel"), rs.getString("cpf_responsavel"), rs.getString("telefone_responsavel"), rs.getString("celular_responsavel"));
            responsaveis.add(r1);
        }
        rs.close();
        stmt.close();
        return responsaveis;
    }
}
